package cn.javabb.web.controller.biz;

import cn.javabb.common.exception.ParameterException;
import cn.javabb.common.model.ResponseModel;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.entity.blog.BlogTypeDO;
import cn.javabb.core.model.StatusEnum;
import cn.javabb.core.service.blog.BlogTypeService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2019/12/03 10:13
 */
@Controller
@RequestMapping("/blogType")
public class BlogTypeController extends BaseController {
    @Autowired
    private BlogTypeService blogTypeService;

    /**
     * 新增Blogtype
     * @param type
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/")
    @ResponseBody
    public Object add(BlogTypeDO type){
        if (null == type) {
            throw new ParameterException();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("typeName", type.getTypeName().trim());
        if (blogTypeService.queryRepeat(BlogTypeDO.class, "id", type.getId(), map)) {
            return ResponseModel.Failure("分类不能重复");
        }
        int num = 0;
        if (null == type.getId()) {
            //insert
            type.setState(StatusEnum.NORMAL.getCode());
            num = blogTypeService.insertSelective(type);
        } else {
            // update
            num = blogTypeService.updateSelective(type);
        }
        if (num > 0) {
            return ResponseModel.Success(type);
        }
        return ResponseModel.Failure("保存失败");
    }

}
